var onSessionOut = false;
var currentUser;

$(function() {
	$.ajax({
		url : "user/currentUser.thor",
		type : "GET",
		dataType : "json",
		cache : false,
		ifModified : true,
		success : function(data) {
			currentUser = data.data.user;
			$("#currentUser").html(currentUser.name);
		},
		error : function(response) {
			showMsgFade();
		}
	});

	/**
	 * 加载菜单
	 */

	$.ajax({
		url : "user/menu.thor",
		type : "POST",
		dataType : "json",
		async : false,
		cache : false,
		ifModified : true,
		success : function(data) {
			parseTree(data);
		},
		error : function(response) {
			showMsgFade();
		}
	});

	/**
	 * 菜单事件
	 */
	var meunDivs = $("#menu").first("div").find("ul").find("li").find("div");
	meunDivs.bind("mouseover", function() {
		$(this).addClass("hover");
	});
	meunDivs.bind("mouseout", function() {
		$(this).removeClass("hover");
	});
	meunDivs.bind("click", function() {
		$(meunDivs).removeClass("selected");
		$(this).addClass("selected");
		var that = this;
		var innerText = $(this).find("a").text();
		var url = $(that).attr("data-url");
		var tab = $("#tabs").tabs('getTab', innerText);
		var content = '<iframe scrolling="yes" frameborder="0"  src="' + url
				+ '" style="width:100%;height:99%;"></iframe>';
		if (!tab) {
			$('#tabs').tabs('add', {
				title : innerText,
				content : content,
				closable : true
			});

		} else {
			$('#tabs').tabs('select', innerText);
		}
	});

	$("#logout").bind('click', function() {
		$.messager.confirm('退出系统', '是否确认退出系统？', function(r) {
			if (r) {
				$.get('user/logout.thor', function(data) {
					location.href = 'index.html';
				});
			}
		});

	});

	window.onfocus = function() {

		if (onSessionOut == true) {
			return;
		}

		var currentUser;
		$.ajax({
			url : "user/currentUser.thor",
			type : "GET",
			dataType : "json",
			cache : false,
			ifModified : true,
			success : function(data) {
			},
			error : function(response) {
				$.messager.alert('操作超时', '操作超时，请重新登陆。', 'info', function() {
					location.href = "index.html";
				});
			}
		});
	}
});

function parseTree(resp) {
	menusObj = resp.data.actions;
	for (var i = 0; i < menusObj.length; i++) {
		var div = " <div title='" + menusObj[i].name + "'>" + "<ul>";
		var lis = "";
		for (var j = 0; j < menusObj[i].children.length; j++) {
			lis += "<li><div class='' data-url='" + menusObj[i].children[j].url
					+ "'><a href='javascript:void(0);'>"
					+ menusObj[i].children[j].name + "</a></div></li>";
		}
		div += lis + "</ul></div>";
		$("#menu").append(div);
	}
}
// 获取角色
function getRole() {
	var resp;
	$.ajax({
		url : "role/getRole.thor",
		type : "GET",
		dataType : "json",
		async : false,
		cache : false,
		ifModified : true,
		success : function(data) {
			resp = JSON.parse(data);
		},
		error : function(response) {
			showMsgFade();
		}
	});
	return resp;
}